﻿@using StackExchange.Opserver.Data.HAProxy
@model HAProxyGroup
@{
    var g = Model;
    var proxies = g.GetProxies();
    var downInstances = g.Instances.Where(i => i.Proxies.LastPollStatus == FetchStatus.Fail);
}
<div class="item haproxy-item @g.MonitorStatus.Class()">
    <div class="server-name" title="Last Updated: @g.Instances.Max(i => i.LastPoll).ToRelativeTime()">
        @g.IconSpan()
        <a href="/haproxy?group=@g.Name" class="node-name-link">@g.Name</a>
        <span class="category">@(proxies.Count.Pluralize("proxy"))</span>
    </div>
    <div class="proxy-list">
        @foreach (var i in downInstances)
        {
            <div>
                @i.MonitorStatus.IconSpan() @i.Name: @i.MonitorStatus.Span(string.Format("Last check failed ({0})", i.LastPoll.ToRelativeTime()))
            </div>
        }
        @if (proxies.Any())
        {
            var groups = proxies.Where(p => p.Servers.Any()).GroupBy(p => p.NiceName).OrderBy(gr => gr.Key);
            foreach (var gr in groups)
            {
                <div>
                    @gr.GetWorstStatus().IconSpan() @gr.Key:
                    @string.Join(", ", 
                        gr.Select(p =>
                            {
                                var down = p.Servers.Where(s => s.ProxyServerStatus.IsBad());
                                var summary = p.Instance.Name + ": " + p.Servers.Count;
                                return down.Any()
                                           ? summary + ", " + MonitorStatus.Critical.Span(down.Count().ToComma() + " down")
                                           : summary;
                            })).AsHtml()
                </div>
            }
        }
        else if(!downInstances.Any())
        {
            <div class="no-content">No proxies found</div>
        }
    </div>
</div>